from singleton import SingletonBase
import configparser
    
class ConfigManager(SingletonBase):
    def __init__(self, config_file):
        self.config = configparser.ConfigParser()
        self.config_file = config_file
        self.load_config()

    def load_config(self):
        """加载配置文件"""
        self.config.read(self.config_file,encoding='utf-8')
    def save_config(self):
        """保存配置文件"""
        with open(self.config_file, 'w',encoding='utf-8') as configfile:
            self.config.write(configfile)
    def get_option(self, section, option):
        """获取配置项的值"""
        return self.config.get(section, option)

    def set_option(self, section, option, value,saveImmediately =False):
        """设置配置项的值"""
        if not self.config.has_section(section):
            self.config.add_section(section)
        self.config.set(section, option, value)
        if saveImmediately:
            self.save_config()
    
    def get_options(self, section):
        """获取配置项的 全部值"""
        return  dict(self.config.items(section))
    def paths(self):
        '''获取PATH 下的 spath、dpath、epath、ppath '''
        return dict(self.get_options("PATH"))

if __name__=='__main__':
    # 使用示例
    config_manager = ConfigManager('config.ini')

    # 读取配置
    value = config_manager.get_options('PATH')
    print(value)


